package com.ys.controller.web.hr;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import com.ys.config.MenuId;
import com.ys.dto.SubGridConfigDto;
import com.ys.entity.hr.ThrVatApplyDetail;
import com.ys.entity.hr.ThrVatApplyDetailCbb;
import com.ys.enume.flow.YsProcInstEnum;
import com.ys.service.flow.YspTaskService;
import com.ys.service.sys.CommonApiService;
import com.ys.vo.W;

@Controller
@RequestMapping("/web/hr/thrVatApplyDetail")
public class WebThrVatApplyDetailController {
	
	@Autowired
	private CommonApiService<ThrVatApplyDetail> commonApiService;
	@Autowired
	private YspTaskService yspTaskService;
	
	/*
	 * 编辑
	 */
	@RequiresPermissions("thrVatApply:add")
	@GetMapping("/toEdit")
	public String toEdit(Model model, HttpServletRequest request, HttpServletResponse response,
			Long id){
		if (id == null) {
			return W.error(model, "ID不能为空");
		}
		
		ThrVatApplyDetail detail = new ThrVatApplyDetail().setId(id).queryFirst();

		Map<String, Object> detail1 = new ThrVatApplyDetail().setId(id)
				.addLeftJoin("THR_OperateCompanyEuropeTrade B1 on B1.VatNo = A.VatNo")
				.addLeftJoin("THR_OperateCompanyApply B2 on B2.Vgsbm = B1.Vgsbm")
				.addField("B2.Vgsbm vgsbm")
				.addLeftJoin("TB_Country B3 on B3.Code = B1.CountryCode")
				.addField("B3.Code countryCode")
				.addField("B3.Name countryName")
				.queryFirstMap();
		
		try {
			// 子列表配置信息
			List<Map<String, Object>> sonlist = new ThrVatApplyDetailCbb()
					.setVatApplyDetailId(id)
					
//					.addLeftJoin("TS_FilesInfo B1 ON B1.Id = A.CbbId")
//						.addField("B1.BidLogsticsOrder bidLogsticsOrder")
//						.addField("B1.StateShenbao stateShenbao")
//					.addField("B1.FilePath filePath")
//
//					.addLeftJoin("TP_FbaLogsticsOrder B ON B.Bid = B1.BidLogsticsOrder")
//						.addField("B.CodeLogsticsServer codeLogsticsServer")
//						.addField("B.EntryNumber entryNumber")
//
//					.addLeftJoin("TP_AmazonShop D ON D.Guid = B.SellerId")
//					.addLeftJoin("TP_AmazonSeller E ON E.id = D.amazonSellerId")
//					.addField("E.Vgsbm vgsbm")
//					.addField("E.Vgsjc vgsjc")

					.addLeftJoin("TCC_LogisticsSadFile B ON B.Id = A.CbbId")
					.addField("b.logInvoiceNo")
					.addField("b.annexFile annexFile")
					.addField("b.declareTime")
					.addField("case when b.declareState =0 then '未申报' else '已申报' end as declareStateName")
					.addField("b.fbaNo")
					.addLeftJoin("THR_OperateCompanyApply E ON E.Vgsbm = B.Vgsbm")
					.addField("E.Vgsjc vgsjc")
					.addField("E.Vgsbm vgsbm")
					.addLeftJoin("TB_FileInfo B1 ON B1.SourceId = A.CbbId AND B1.SourceType = 46")
					.addField("B1.FilePath filePath")
					.queryMap();
			SubGridConfigDto subGridConfig = commonApiService.getSubGrid(MenuId.THR_VatApplyDetail, sonlist);
			
			// 控制流转中没有子列表按钮
			Integer stateFlow = yspTaskService.flowState(detail.getPbid());
			if (stateFlow != YsProcInstEnum.StateFlow.INIT.value) {
				subGridConfig.setButtonArr(new ArrayList<>());
			}
			
			return commonApiService.toEdit(model, id, new ThrVatApplyDetail(), MenuId.THR_VatApplyDetail, detail1, subGridConfig);
		} catch (Exception e) {
			e.printStackTrace();
			return W.error(model, "数据有误，请联系客服");
		}
	}
}